const fs = require('fs')
const path = require('path')

let bucketStr = ''
const deployEnv = process.env.DREAMENV
const date = new Date()
const prefix = `${process.env.CI_PROJECT_NAME}/${
  date.getFullYear()}${`0${date.getMonth() + 1}`.slice(-2)}${`0${date.getDate()}`.slice(-2)}/`
const config = {
  src_dir: path.resolve('dist').replace(/\\/g, '/'),
  bucket: '',
  key_prefix: '',
  up_host: 'http://up-z2.qiniup.com',
  overwrite: true,
  skip_fixed_strings: '.svn,.git',
  skip_suffixes: '.DS_Store,.exe',
  log_stdout: true,
}

switch (deployEnv) {
  case 'TEST':
    bucketStr = process.env.QINIU_TEST
    break
  case 'PROD':
    bucketStr = process.env.QINIU_PROD
    break
  default:
    break
}

if (!bucketStr) {
  throw new Error('config get error')
}
const bucketConfig = JSON.parse(bucketStr)
config.bucket = bucketConfig.bucket
config.key_prefix = prefix
fs.writeFileSync('qiniuconfig', JSON.stringify(config))
